package com.eam.eventhandler;

import com.eam.data.EamFacilitiesCheck;
import com.eam.data.EamCheckPlanItem;
import com.eam.data.EamCheckPlanFacilities;
import org.hibernate.Query;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.database.SessionInfo;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;
import java.util.List;

//点检计划下的设备层
public class FacilitiesItemEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamCheckPlanFacilities.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}

	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
		
		//获取修改的记录
		EamCheckPlanFacilities item = (EamCheckPlanFacilities) event.getTargetInstance();
		
		String plan_id = item.getEamCheckPlan().getId(); //计划ID
		
		String f_id = item.getEamFacilities().getId(); //设备ID
		
		String status = item.getEamCheckPlan().getState(); //计划状态
		
		String itemStatus = item.getStatus(); //设备状态
		
		if(status.equals("OK")){
			String language = OBContext.getOBContext().getLanguage().getLanguage();
	        ConnectionProvider conn = new DalConnectionProvider(false);
	        throw new OBException(Utility.messageBD(conn, "该计划已点检完，不可修改点检设备！", language));
		}else{
			
			if(itemStatus.equals("OK")){
				String language = OBContext.getOBContext().getLanguage().getLanguage();
		        ConnectionProvider conn = new DalConnectionProvider(false);
		        throw new OBException(Utility.messageBD(conn, "该设备已点检完，不可修改点检设备！", language));
			}else{
				
				
				String sql = "select t from Eam_Check_Facilities t where  t.EamFacilities.id = :f_id and t.EamCheckPlan.id = :plan_id";
				Query query = OBDal.getInstance().getSession().createQuery(sql);
				query.setString("f_id", f_id);
				query.setString("plan_id", plan_id);
				
				if(query.list().size() > 1){
					String language = OBContext.getOBContext().getLanguage().getLanguage();
			        ConnectionProvider conn = new DalConnectionProvider(false);
			        throw new OBException(Utility.messageBD(conn, item.getEamFacilities().getName()+"已经在该计划中，请仔细检查！！", language));
				}else{
					
					//删除修改之前的设备下的点检项目
					String eql = "delete from Eam_Check_Plan_Item where Eam_Check_Plan_Facilities_id = ? ";
					
					OBDal.getInstance().getSession().createSQLQuery(eql).setString(0, item.getId()).executeUpdate();
					
					//获取设备维修、保养、點檢项目配置里设备配置的點檢项目并保存下来
					String aql = "select dj from Eam_Facilities_Check dj where dj.EamFacilities.id = :f_id ";
					Query querys = OBDal.getInstance().getSession().createQuery(aql);
					querys.setString("f_id", f_id);
					
					List<EamFacilitiesCheck> list = querys.list();
					
					for(EamFacilitiesCheck mains : list){
						EamCheckPlanItem details = new EamCheckPlanItem(); //计划下设备下的项目层(第三层)	
						details.setEamCheckPlanFacilities(item); 
						details.setEamCheckItem(mains.getEamCheckItem());
						OBDal.getInstance().save(details);
					}
				}
			}
		}
	}
	
	//新增
	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		String tabId = SessionInfo.getProcessId();

		if (tabId == null) {
			return;
		}
		
		//获取新增的记录
		EamCheckPlanFacilities item = (EamCheckPlanFacilities) event.getTargetInstance();
		
		String plan_id = item.getEamCheckPlan().getId(); //计划ID
		
		String f_id = item.getEamFacilities().getId(); //设备ID
		
		String status = item.getEamCheckPlan().getState(); //计划状态
		
		if(status.equals("OK")){
			String language = OBContext.getOBContext().getLanguage().getLanguage();
	        ConnectionProvider conn = new DalConnectionProvider(false);
	        throw new OBException(Utility.messageBD(conn, "该计划已点检完，不可新增点检设备！", language));
		}else{
			
			//获取设备维修、保养、點檢项目配置里设备配置的點檢项目并保存下来
			String aql = "select dj from Eam_Facilities_Check dj where dj.EamFacilities.id = :f_id ";
			Query querys = OBDal.getInstance().getSession().createQuery(aql);
			querys.setString("f_id", f_id);
			
			List<EamFacilitiesCheck> list = querys.list();
			
			for(EamFacilitiesCheck mains : list){
				EamCheckPlanItem details = new EamCheckPlanItem(); //计划下设备下的项目层(第三层)	
				details.setEamCheckPlanFacilities(item); 
				details.setEamCheckItem(mains.getEamCheckItem());
				OBDal.getInstance().save(details);
				
				
			}
			
			String sql = "select t from Eam_Check_Facilities  t where  t.EamFacilities.id = :f_id and t.EamCheckPlan.id = :plan_id";
			Query query = OBDal.getInstance().getSession().createQuery(sql);
			query.setString("f_id", f_id);
			query.setString("plan_id", plan_id);
			
			if(query.list().size() > 0){
				String language = OBContext.getOBContext().getLanguage().getLanguage();
		        ConnectionProvider conn = new DalConnectionProvider(false);
		        throw new OBException(Utility.messageBD(conn, item.getEamFacilities().getName()+"已经在该计划中，请仔细检查！！", language));
			}
			
		}
		
		
	

	}

	public void onDelete(@Observes EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}

}
